package com.lvmama.pangolin.core;

import com.lvmama.pangolin.entity.core.BaseCommodity;
import com.lvmama.pangolin.entity.core.BaseProduct;
import com.lvmama.pangolin.mybatis.mapper.BaseCommodityMapper;
import com.lvmama.pangolin.util.CommonCommodityUtils;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * 通用商品service类
 * <h1>@Author:wangpeng</h1>
 * <P>为了增加精确度，相似度统一乘上100</P>
 * @param <T>
 */
public abstract class AbstractBaseCommodityService<T extends BaseCommodity>  implements BaseService<T>,CommonCommodityService  {

    @Autowired
    protected BaseMapper<T> mapper;

    @Autowired
    public CommonCommodityUtils commonCommodityUtils;

    @Autowired
    public BaseCommodityMapper baseCommodityMapper;

    private Class<T> modelClass;    // 当前泛型真实类型的Class

    public AbstractBaseCommodityService() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        modelClass = (Class<T>) pt.getActualTypeArguments()[0];
    }

    public int save(T model) {
        return mapper.save(model);
    }

    public int saveList(List<T> models) {
        return mapper.saveList(models);
    }

    public int deleteByMap(Map<String,String> paramMap) {
        return mapper.deleteByMap(paramMap);
    }

    public int deleteById(String id) {
        return mapper.deleteById(id);
    }

    public int update(T model) {
        return mapper.update(model);
    }

    public T findById(String id) {
        return mapper.findById(id);
    }

    @Override
    public T findByObject(T model) throws TooManyResultsException {
        return mapper.findByObject(model);
    }

    public List<T> findByIds(String ids) {
        return mapper.findByIds(ids);
    }

    public List<T> findListByMap(Map<String,String> paramMap) {
        return mapper.findListByMap(paramMap);
    }

    public List<T> findAll() {
        return mapper.findAll();
    }

    /**
     * 两个商品名称相似度
     * @param base：标准商品
     * @param compareTo
     * @return
     */
    @Override
    public double nameSimilarity(BaseCommodity base, BaseCommodity compareTo) {
        return commonCommodityUtils.nameScore(base.getCommodityName(),compareTo.getCommodityName())*ONE_HUNDRED;
    }
    /**
     * 两个商品类型相似度
     * @param base：标准商品
     * @param compareTo
     * @return
     */
    @Override
    public double typeSimilarity(BaseCommodity base, BaseCommodity compareTo) {
        return commonCommodityUtils.typeScore(base.getCommodityType(),compareTo.getCommodityType())*ONE_HUNDRED;
    }
    /**
     * 两个商品价格相似度
     * @param base：标准商品
     * @param compareTo
     * @return
     */
    @Override
    public double priceSimilarity(BaseCommodity base, BaseCommodity compareTo) {
        return commonCommodityUtils.priceScore(base.getSalePrice().trim(),compareTo.getSalePrice().trim())*ONE_HUNDRED;
    }
    /**
     * 两个商品城市相似度
     * @param base：标准商品
     * @param compareTo
     * @return
     */
    @Override
    public double citySimilarity(BaseCommodity base, BaseCommodity compareTo) {
        return commonCommodityUtils.cityScore(base.getCityCode().trim(),compareTo.getCityCode().trim())*ONE_HUNDRED;
    }

    /**
     * 传入的两个字符串的相似度
     * @param baseName
     * @param compareToName
     * @return
     */
    public double scoreString(String baseName ,String compareToName)
    {
        return commonCommodityUtils.nameScore(baseName,compareToName)*ONE_HUNDRED;
    }

    /**
     * 根据通用产品查询通用商品
     * @param baseProduct
     * @return
     */
    public List<BaseCommodity> selectCommodityByBaseProduct(BaseProduct baseProduct)
    {
        return baseCommodityMapper.selectCommodityByBaseProduct(baseProduct);
    }
}
